package com.bhkj.pdjhbgnewproject.core.service;

import android.arch.lifecycle.ViewModelProviders;

import com.bhkj.pdjhbgnewproject.R;
import com.bhkj.pdjhbgnewproject.common.bean.PersonInfoBean;
import com.bhkj.pdjhbgnewproject.common.contanct.Contacts;
import com.bhkj.pdjhbgnewproject.common.utils.Tools;
import com.bhkj.pdjhbgnewproject.core.main.MainActivity;
import com.bhkj.pdjhbgnewproject.core.viewmodel.MainViewRealModel;
import com.bhkj.pdjhbgnewproject.core.viewmodel.WindowStatusModel;
import com.bhkj.websocketlibrary.socketio.ISocketIoCallback;
import com.bhkj.websocketlibrary.socketio.SocketIoManager;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

public class WsDataManager {

    public WsDataManager() {
//        WsClient.getInstance().connect();
//        WsClient.getInstance().addOnCallback(new WsClient.IWsCallback() {
//            @Override
//            public void onMessage(String msg) {
//                diliverData(msg);
//            }
//        });
        initSocket();
    }

    private void initSocket() {
        SocketIoManager instance = SocketIoManager.getInstance();
        instance.addCallback(new ISocketIoCallback() {
            @Override
            public void onLogin(final String login) {
                diliverData(login);
            }

            @Override
            public void onLogout(String logout) {
                diliverData(logout);
            }

            @Override
            public void onWindowStatus(String status) {
                diliverData(status);
            }

            @Override
            public void onOther(String res, String otherType) {

            }
        });
    }

    private void diliverData(String msg) {
        String serverMsg = Tools.AESUtil.decrypt(msg, StringUtils.getString(R.string.mm));
        LogUtils.d("测试：" + serverMsg);

        if (ObjectUtils.isEmpty(serverMsg)) {
            ToastUtils.showLong("WS推送数据为空!");
            return;
        }

        if (serverMsg.contains(Contacts.PUSH_LOGIN)) {
            login(serverMsg);
        } else if (serverMsg.contains(Contacts.PUSH_LOGOUT)) {
            changeWidowStatus(Contacts.PUSH_LOGOUT);
        } else if (serverMsg.contains(Contacts.PUSH_STOP_SERVICE) || serverMsg.contains(Contacts.PUSH_END_SERVICE)) {
            changeWidowStatus(Contacts.PUSH_END_SERVICE);
        } else if (serverMsg.contains(Contacts.PUSH_RESUME_SERVICE)) {
            changeWidowStatus(Contacts.PUSH_RESUME_SERVICE);
        }
    }



    private void changeWidowStatus(final String windowStatus) {
        MainActivity topActivity = (MainActivity) ActivityUtils.getTopActivity();
        SPUtils instance = SPUtils.getInstance();
        if (topActivity != null) {
            final WindowStatusModel windowStatusModel = ViewModelProviders.of(topActivity).get(WindowStatusModel.class);
            topActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        windowStatusModel.setWindowStatus(windowStatus);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            if (ObjectUtils.equals(windowStatus,Contacts.PUSH_LOGOUT)){
                instance.put(Contacts.WINDOW_STATUS,Contacts.PUSH_LOGOUT);
            }else if (ObjectUtils.equals(windowStatus,Contacts.PUSH_STOP_SERVICE)||
                    ObjectUtils.equals(windowStatus,Contacts.PUSH_END_SERVICE)){
                instance.put(Contacts.WINDOW_STATUS,Contacts.PUSH_END_SERVICE);
            }else if (ObjectUtils.equals(windowStatus,Contacts.PUSH_RESUME_SERVICE)){
                instance.put(Contacts.WINDOW_STATUS,Contacts.PUSH_RESUME_SERVICE);

            }
        }
    }

    private void login(final String msg) {
        MainActivity topActivity = (MainActivity) ActivityUtils.getTopActivity();
        if (topActivity != null) {
            LogUtils.d("执行了发送");
            final MainViewRealModel mainViewRealModel = ViewModelProviders.of(topActivity).get(MainViewRealModel.class);
            topActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        mainViewRealModel.setPersonInfo(GsonUtils.fromJson(msg, PersonInfoBean.class));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            SPUtils.getInstance().put(Contacts.WINDOW_STATUS,Contacts.PUSH_LOGIN);
        }
    }

}
